cmake_minimum_required(VERSION 2.8)

project(sg_agent C CXX)
SET(CMAKE_CXX_FLAGS "-g -fPIC -Wno-deprecated -DHAVE_NETINET_IN_H -DMUDUO_STD_STRING")

SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

#for filter absolute path of compiler machine
SET(CMAKE_USE_RELATIVE_PATHS ON)

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
MESSAGE(STATUS "CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}")

IF(ENABLE_COVERAGE)
    include(CodeCoverage)
    APPEND_COVERAGE_COMPILER_FLAGS()
ENDIF()

#use clog
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -DUSE_CLOG_SUPPORT")

SET(AGENT_SRC_PATH ${CMAKE_SOURCE_DIR}/src)
MESSAGE(STATUS "AGENT_SRC_PATH ${AGENT_SRC_PATH}")

SET(SGAGENT_MODULE_PATH ${CMAKE_SOURCE_DIR}/thrid_party)
MESSAGE(STATUS "SGAGENT_MODULE_PATH ${SGAGENT_MODULE_PATH}")

SET(THIRD_MODULE_PATH ${CMAKE_SOURCE_DIR}/thrid_party)
MESSAGE(STATUS "THIRD_MODULE_PATH ${THIRD_MODULE_PATH}")

SET(COMMON_PATH ${AGENT_SRC_PATH}/comm)
MESSAGE(STATUS "COMMON_PATH ${COMMON_PATH}")

SET(COMMONLOG_PATH ${AGENT_SRC_PATH}/commonlog)
MESSAGE(STATUS "COMMONLOG_PATH ${COMMONLOG_PATH}")

SET(UTIL_PATH ${AGENT_SRC_PATH}/util)
MESSAGE(STATUS "UTIL_PATH ${UTIL_PATH}")

SET(RULE_PATH ${AGENT_SRC_PATH}/rule)
MESSAGE(STATUS "RULE_PATH ${RULE_PATH}")

SET(MCC_PATH ${AGENT_SRC_PATH}/mcc)
MESSAGE(STATUS "MCC_PATH ${MCC_PATH}")

SET(MNS_PATH ${AGENT_SRC_PATH}/mns)
MESSAGE(STATUS "MNS_PATH ${MNS_PATH}")

SET(MAFKA_PATH ${AGENT_SRC_PATH}/remote/mafka)
MESSAGE(STATUS "MAFKA_PATH ${MAFKA_PATH}")


SET(FALCON_PATH ${AGENT_SRC_PATH}/remote/falcon)
MESSAGE(STATUS "FALCON_PATH ${FALCON_PATH}")

SET(LOG4CPLUS_PATH ${CMAKE_SOURCE_DIR}/thrid_party/logs/log4cplus)
MESSAGE(STATUS "LOG4CPLUS_PATH ${LOG4CPLUS_PATH}")

SET(MONITOR_COLLECTOR_PATH ${AGENT_SRC_PATH}/remote/monitor)
MESSAGE(STATUS "MONITOR_COLLECTOR_PATH ${MONITOR_COLLECTOR_PATH}")

INCLUDE(ExternalProject)

#for output
EXECUTE_PROCESS(COMMAND mkdir ${CMAKE_SOURCE_DIR}/build)
SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/build)
MESSAGE(STATUS "CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}")

#default binary and lib path
SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_INSTALL_PREFIX}/bin)
EXECUTE_PROCESS(COMMAND mkdir ${EXECUTABLE_OUTPUT_PATH})
MESSAGE(STATUS "EXECUTABLE_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH}")

# find muduo libraries
if (CMAKE_BUILD_INIT)
	    FIND_PACKAGE(Muduo REQUIRED)
      MESSAGE(STATUS "MUDUO_INCLUDE_DIR ${MUDUO_INCLUDE_DIR}")
      MESSAGE(STATUS "MUDUO_BASE_LIBRARIES ${MUDUO_BASE_LIBRARY}")
else()
      find_library(MUDUO_BASE_LIBRARY libmuduo_base.a)
      find_library(MUDUO_NET_LIBRARY libmuduo_net.a)
   	  find_library(MUDUO_HTTP_LIBRARY libmuduo_http.a)
      MESSAGE(STATUS,"MUDUO_BASE_LIBRARY ${MUDUO_BASE_LIBRARY}")
      MESSAGE(STATUS,"MUDUO_NET_LIBRARY ${MUDUO_NET_LIBRARY}")
      MESSAGE(STATUS,"MUDUO_HTTP_LIBRARY ${MUDUO_HTTP_LIBRARY}")
      # find zookeeper libraries
      FIND_PACKAGE(Zookeeper REQUIRED)
      MESSAGE(STATUS "ZOOKEEPER_LIBRARY ${ZOOKEEPER_LIBRARY}")
      MESSAGE(STATUS "ZOOKEEPER_INCLUDE_DIR ${ZOOKEEPER_INCLUDE_DIR}")

      # find boost libraries
      FIND_PACKAGE(Boost REQUIRED)
      MESSAGE(STATUS "BOOST_INCLUDE_DIR ${BOOST_INCLUDE_DIR}")

      #find cthrift libraries
      FIND_PACKAGE(Cthrift REQUIRED)
      MESSAGE(STATUS "CTHRIFT_INCLUDE_DIR ${CTHRIFT_INCLUDE_DIR}")
      MESSAGE(STATUS "CTHRIFT_LIBRARY ${CTHRIFT_LIBRARY}")

      #find cthrift libraries
      FIND_PACKAGE(Sdk REQUIRED)
      #MESSAGE(STATUS "CTHRIFT_INCLUDE_DIR ${CTHRIFT_INCLUDE_DIR}")
      MESSAGE(STATUS "MNS_SDK_LIBRARY ${MNS_SDK_LIBRARY}")

      FIND_PACKAGE(Log4cplus REQUIRED)
      MESSAGE(STATUS "LOG4CPLUS_INCLUDE_DIR ${LOG4CPLUS_INCLUDE_DIR}")
      MESSAGE(STATUS "LOG4CPLUS_LIBRARY ${LOG4CPLUS_LIBRARY}")

      # find thrift libraries
      FIND_PACKAGE(Thrift REQUIRED)
      SET(THRIFT_COMPILER ${THIRD_MODULE_PATH}/thrift-0.8.0_build/bin/thrift)
      SET(THRIFT_INCLUDE_DIR ${SGAGENT_MODULE_PATH}/thrift/include)
      SET(THRIFT_STATIC_LIB ${SGAGENT_MODULE_PATH}/thrift/lib/libthrift.a)
      SET(THRIFTNB_STATIC_LIB ${SGAGENT_MODULE_PATH}/thrift/lib/libthriftnb.a)
      MESSAGE(STATUS "THRIFT_INCLUDE_DIR ${THRIFT_INCLUDE_DIR}")
      MESSAGE(STATUS "THRIFT_LIBS ${THRIFT_LIBS}")
      MESSAGE(STATUS "THRIFT_STATIC_LIB ${THRIFT_STATIC_LIB}")
      MESSAGE(STATUS "THRIFTNB_STATIC_LIB ${THRIFTNB_STATIC_LIB}")

      SET(OCTO_IDL_PATH ../common/idl-mns)
      MESSAGE(STATUS "OCTO_IDL_PATH ${OCTO_IDL_PATH}")

      SET(CURRENT_CMAKE_PATH )
      SET(THRIFT_GEN_CPP_PATH ../../../octo-open_source/common/gen-cpp)
      MESSAGE(STATUS "THRIFT_GEN_CPP_PATH ${THRIFT_GEN_CPP_PATH}")
 
      INCLUDE_DIRECTORIES(system ${CTHRIFT_INCLUDE_DIR})
      INCLUDE_DIRECTORIES(system ${SDK_INCLUDE_DIR})

      MESSAGE(STATUS "CTHRIFT_INCLUDE_DIR ${CTHRIFT_INCLUDE_DIR}")
      MESSAGE(STATUS "SDK_INCLUDE_DIR ${SDK_INCLUDE_DIR}")

    if(${CMAKE_BUILD_TYPE} MATCHES "debug")
        ADD_DEFINITIONS(-DSG_AGENT_TEST)
        ADD_SUBDIRECTORY(test)
    else()
        ADD_SUBDIRECTORY(src/mns)
    endif()

INSTALL(CODE "execute_process(COMMAND find ${CMAKE_SOURCE_DIR}/tool/ -type f -exec cp -rt ${EXECUTABLE_OUTPUT_PATH} {} \;)")
INSTALL(CODE "execute_process(COMMAND find ${CMAKE_SOURCE_DIR}/conf/ -type f -exec cp -rt ${EXECUTABLE_OUTPUT_PATH} {} \;)")

endif()
